package tech.aistar.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;
import tech.aistar.entity.Myorder;
import tech.aistar.entity.Product;
import tech.aistar.service.IMyorderService;
import tech.aistar.service.IProductService;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;
@Controller
public class MyOrderController {

    @Autowired
    private IMyorderService myorderService;
    @Autowired
    private IProductService productService;


    //保存订单
    @PostMapping("order")  // post: http://localhost:8080/hirecar/order
    public ModelAndView save(Integer proId, BigDecimal proPrice) {
        Myorder order = new Myorder();
        order.setOrderNumber(UUID.randomUUID().toString().replace("-", ""));
        order.setCustId(1);
        order.setProId(proId);
        order.setOrderPrice(proPrice);
        order.setOrderCreateTime(LocalDateTime.now());
        order.setStatus(1);
        myorderService.save(order);
        Product product = productService.getById(order.getProId());
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("order", order);
        modelAndView.addObject("product", product);
        modelAndView.setViewName("confirm");
        return modelAndView;
    }

    //查看所有订单
    @GetMapping("allorder")
    public ModelAndView getAll(Integer customerId){
        customerId=1;
        ModelAndView modelAndView=new ModelAndView();
        List<Myorder> myorderList = myorderService.getByCustomerId(customerId);
        for(Myorder myorder:myorderList){
            String proName  = productService.getById(myorder.getProId()).getProName();
            myorder.setOther1(proName);
        }

        modelAndView.addObject("myorderList",myorderList);
        modelAndView.setViewName("allorder");
        return modelAndView;
    }
}
